/**
 * 
 */
/**
 * @author Administrator
 *
 */
package com.ruoyi.door.utils.baofu.http;

import com.alibaba.fastjson2.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

import java.io.UnsupportedEncodingException;
import java.util.Map;


public class HttpUtil{
	public static String RequestForm(String Url,Object Parms) throws Exception
	{
		if(null == Parms) throw new Exception("参数不能为空！");
		try {
			HttpSendModel httpSendModel = new HttpSendModel(Url,Parms);
			httpSendModel.setMethod(HttpMethod.POST);
			SimpleHttpResponse response = null;
			response = doRequest(httpSendModel, "utf-8");
			return response.getEntityString();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
	}
	public static String RequestForm(String Url,Map<String,String> Parms) throws Exception
	{
		if(Parms.isEmpty()){
			return  "参数不能为空！";
		}
		try {
			HttpSendModel httpSendModel = new HttpSendModel(Url,Parms);
			httpSendModel.setMethod(HttpMethod.POST);
			SimpleHttpResponse response = null;
			response = doRequest(httpSendModel, "utf-8");
			return response.getEntityString();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}

	}
	
	public static SimpleHttpResponse doRequest(HttpSendModel httpSendModel, String getCharSet) throws Exception
	{
		// 创建默认的httpClient客户端端
		SimpleHttpClient simpleHttpclient = new SimpleHttpClient();

		try {
			return doRequest(simpleHttpclient, httpSendModel, getCharSet);
		} finally {
			simpleHttpclient.getHttpclient().getConnectionManager().shutdown();
		}

	}

	/**
	 * HTTP请求
	 * @param simpleHttpclient
	 * @param httpSendModel
	 * @param getCharSet
	 * @return
	 * @throws Exception
	 */
	public static SimpleHttpResponse doRequest(SimpleHttpClient simpleHttpclient, HttpSendModel httpSendModel,
                                               String getCharSet) throws Exception {

		HttpRequestBase httpRequest = buildHttpRequest(httpSendModel);

		if (httpSendModel.getUrl().startsWith("https://")) {
			simpleHttpclient.enableSSL();
		}

		try {
			HttpResponse response = simpleHttpclient.getHttpclient().execute(
					httpRequest);
			int statusCode = response.getStatusLine().getStatusCode();

			if (isRequestSuccess(statusCode)) {
				return new SimpleHttpResponse(statusCode, EntityUtils.toString(
						response.getEntity(), getCharSet), null);
			} else {
				return new SimpleHttpResponse(statusCode, null, response
						.getStatusLine().getReasonPhrase());
			}

		} catch (Exception e) {
			throw new Exception("http请求异常", e);
		}

	}

	/**
	 * @param httpSendModel
	 * @return
	 * @throws Exception 
	 */
	protected static HttpRequestBase buildHttpRequest(HttpSendModel httpSendModel) throws Exception {
		HttpRequestBase httpRequest;
		if (httpSendModel.getMethod() == null) {
			throw new Exception("请求方式未设定");
		} else if (httpSendModel.getMethod() == HttpMethod.POST) {

			String url = httpSendModel.getUrl();
			String sendCharSet = httpSendModel.getCharSet();
			HttpPost httppost = new HttpPost(url);
			try {
				if(httpSendModel.getContentType().toLowerCase().contains("x-www-form-urlencoded"))
				{
					UrlEncodedFormEntity PostParm = new UrlEncodedFormEntity(httpSendModel.getHttpparms(),sendCharSet);
					PostParm.setContentType(httpSendModel.getContentType());
					httppost.setEntity(PostParm);
				}else if(httpSendModel.getContentType().toLowerCase().contains("json")){
					StringEntity Str = new StringEntity(JSONObject.toJSONString(httpSendModel.getHttpparms()).toString(), sendCharSet);
					Str.setContentEncoding(sendCharSet);
					Str.setContentType(httpSendModel.getContentType());
					httppost.setEntity(Str);
				}else{
					throw new Exception("ContentType is not exist");
				}
			} catch (UnsupportedEncodingException e) {
				throw new Exception("构建post请求参数失败", e);
			}
			httpRequest = httppost;
		} else if (httpSendModel.getMethod() == HttpMethod.GET) {
			HttpGet httpget = new HttpGet(httpSendModel.buildGetRequestUrl());
			httpRequest = httpget;
		} else {
			throw new Exception("请求方式不支持：" + httpSendModel.getMethod());
		}

		return httpRequest;
	}

	/**
	 * 请求是否成功
	 * 
	 * @param statusCode
	 * @return
	 */
	public static boolean isRequestSuccess(int statusCode) {
		return statusCode == 200;
	}
	
	
	
	
}